package cn.ococci.dm.Base;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import cn.ococci.dm.Main.Activity.MainActivity;
import cn.ococci.dm.Main.Const;
import cn.ococci.dm.Main.Tools.SpTool;
import cn.ococci.dm.Main.Tools.ToastTool;
import cn.ococci.dm.Main.View.TitleBar;
import cn.ococci_dm.R;


/**
 * Created by cyan on 15/12/29.
 */
public abstract class BaseActivity  extends FragmentActivity implements BaseController,FragmentController {

    private TitleBar titleBar;
    private long backStartTime;

    protected boolean visible = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityStack.push(this);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        if(getResId() == 0){
            throw new RuntimeException("资源ID为0");
        }

        this.setContentView(getResId());
        this.initView();
        initData();
        initAdapter();
        initListener();
        initReceiver();
        initTitleBar();
        initViewColor(BaseApp.COLOR);
    }

    protected abstract int getResId();

    protected  abstract void initView();

    protected abstract void initData();

    protected void initAdapter() {}

    protected void initListener() {}

    protected void initReceiver(){}

    protected void destroyReceiver(){}

    protected void initTitleBar() {
        titleBar = (TitleBar) findViewById(R.id.titleBar);
        if (titleBar != null) {
            titleBar.setAdapter(this);
        }
    }

    protected void initViewColor(int color){}


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        visible = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        visible = false;
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onBackPressed() {
        if (!goBack()) {
            long now = System.currentTimeMillis();
            if (now - backStartTime > 3000) {
                ToastTool.showShortTime("再次点击退出！");
            } else {
                if(ActivityStack.currentActivity() instanceof MainActivity){
                    SpTool.putBoolean(Const.LoginParams.ON_LINE,true);
                    moveTaskToBack(true);
                }else{
                    ActivityStack.clear();
                }
            }
            backStartTime = now;
        }
    }

    @Override
    public boolean goBack() {
        if (getLastActivity() != null) {
            backTo(getLastActivity());
            return true;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        destroyReceiver();
        super.onDestroy();
    }


    public boolean isVisible() {
        return visible;
    }

    public Class<? extends BaseActivity> getLastActivity() {
        return null;
    }

    public void backTo(Class<? extends BaseActivity> cls) {
        Intent _intent = new Intent(this, cls);
        this.startActivity(_intent);
        overridePendingTransition(R.anim.activity_goback_enter_anim, R.anim.activity_goback_exit_anim);
        this.exit();
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void turnTo(Class<? extends BaseActivity> cls,int code,Bundle bundle){
        Intent _intent = new Intent(this, cls);
        _intent.putExtras(bundle);
        this.startActivityForResult(_intent,code,bundle);
        overridePendingTransition(R.anim.activity_gonext_enter_anim, R.anim.activity_gonext_exit_anim);
        this.exit();
    }


    public void turnTo(Class<? extends BaseActivity> cls) {
        Intent _intent = new Intent(this, cls);
        this.startActivity(_intent);
        overridePendingTransition(R.anim.activity_gonext_enter_anim, R.anim.activity_gonext_exit_anim);
        this.exit();
    }


    /**
     * 在需要调用finish的位置使用exit
     */
    public void exit() {
        ActivityStack.popActivity(this);
    }

    public void hideInput() { // 不显示键盘
        View v = this.getCurrentFocus();
        if (v != null && v instanceof EditText) {
            InputMethodManager imm = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // 强制隐藏键盘
        }
    }


    @Override
    public BaseActivity getBaseActivity() {
        return this;
    }

    @Override
    public void upDateTitleBar() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                titleBar.updateView();
            }
        });
    }

    @Override
    public void onBackClick() {
        goBack();
    }


    @Override
    public String getBackCaption() {
        return null;
    }

    @Override
    public String getCaption() {
        return null;
    }

    @Override
    public int getCaptionSize() {
        return 0;
    }

    @Override
    public void onRightTextClick() {

    }

    @Override
    public String getRightText() {
        return null;
    }

    @Override
    public int getRightTextSize() {
        return 0;
    }

    @Override
    public void onRightImageClick() {

    }

    @Override
    public int getRightImageResId() {
        return 0;
    }

    @Override
    public String[] getRadioTitles() {
        return null;
    }

    @Override
    public int getRadioDefaultIndex() {
        return 0;
    }

    @Override
    public void onRadioClicked(int index) {
    }

    @Override
    public void setFragment(BaseFragment fragment, int animType) {

    }


    @Override
    public void onCaptionClicked() {

    }
}
